<template>

    <z-paging ref="pagingRef" v-model="list" @query="queryList">
        <view style="z-index: 100;position: sticky;top: 0">
            <slot name="header"></slot>
        </view>
        <view class="bg-gradual-blue nav text-center radius-bottom-lg">
            <view class="cu-item" :class="currentTab===0?'text-white cur':''" @tap="tabSelect" data-id="0">
                <text class="cuIcon-home"></text>
                医院
            </view>
            <view class="cu-item" :class="currentTab===1?'text-white cur':''" @tap="tabSelect" data-id="1">
                <text class="cuIcon-people"></text>
                医护
            </view>
            <view class="cu-item" :class="currentTab===2?'text-white cur':''" @tap="tabSelect" data-id="2">
                <text class="cuIcon-read"></text>
                文章
            </view>
            <view class="cu-item" :class="currentTab===3?'text-white cur':''" @tap="tabSelect" data-id="3">
                <text class="cuIcon-title"></text>
                综合
            </view>
        </view>
        <view v-for="(item,index) in list" :key="index">
            <view @click="toQuery(item)">
                <HospitalItem v-if="currentTab===0" :hospital-item="item"></HospitalItem>
                <NurseItem v-if="currentTab===1" :nurse-item="item"></NurseItem>
                <ArticleItem v-if="currentTab===2" :article-item="item"></ArticleItem>
            </view>
        </view>
        <view v-if="currentTab===3">
            <view v-if="hospitalList.length" class="bg-white margin radius-lg">
                <view class="cu-bar">
                    <view class="action">
                        <text class="cuIcon-titles "></text>
                        <text class="text-xl text-bold">医院</text>
                    </view>
                    <view class="action">
                        <text class="text-lg text-gray" @click="doSearch(currentTab=0,searchKey)">
                            更多
                            <text class="cuIcon-right"></text>
                        </text>
                    </view>
                </view>
                <view v-for="(item,index) in hospitalList" :key="index">
                    <navigator
                            :url="'/pages/common/introduction/hospital?hospitalInfo=' + encodeURIComponent(JSON.stringify(item))">
                        <HospitalItem :hospital-item="item"></HospitalItem>
                    </navigator>
                </view>
            </view>
            <view v-if="nurseList.length" class="bg-white margin radius-lg">
                <view class="cu-bar">
                    <view class="action">
                        <text class="cuIcon-titles "></text>
                        <text class="text-xl text-bold">医护</text>
                    </view>
                    <view class="action">
                        <text class="text-lg text-gray" @click="doSearch(currentTab=1,searchKey)">
                            更多
                            <text class="cuIcon-right"></text>
                        </text>
                    </view>
                </view>
                <view v-for="(item,index) in nurseList" :key="index">
                    <navigator
                            :url="'/pages/common/introduction/nurse?nurseInfo=' + encodeURIComponent(JSON.stringify(item))">
                        <NurseItem :nurse-item="item"></NurseItem>
                    </navigator>
                </view>
            </view>
            <view v-if="articleList.length" class="bg-white margin radius-lg">
                <view class="cu-bar">
                    <view class="action">
                        <text class="cuIcon-titles "></text>
                        <text class="text-xl text-bold">文章</text>
                    </view>
                    <view class="action">
                        <text class="text-lg text-gray" @click="doSearch(currentTab=2,searchKey)">
                            更多
                            <text class="cuIcon-right"></text>
                        </text>
                    </view>
                </view>
                <view v-for="(item,index) in articleList" :key="index">
                    <navigator
                            :url="'/pages/common/article/article?articleInfo=' + encodeURIComponent(JSON.stringify(item))">
                        <ArticleItem :article-item="item"></ArticleItem>
                    </navigator>
                </view>
            </view>
        </view>
    </z-paging>
</template>

<script setup lang="ts">

import {defineExpose, ref} from "vue";
import api from "@/api";
import ArticleItem from "@/components/common/item/ArticleItem.vue";
import NurseItem from "@/components/common/item/NurseItem.vue";
import HospitalItem from "@/components/common/item/HospitalItem.vue";
import {useUserStore} from "@/store";

const userStore = useUserStore()
const currentTab = ref(-1)
const searchKey = ref('')

const list = ref([])
const hospitalList = ref([])
const nurseList = ref([])
const articleList = ref([])
const pagingRef = ref()

const andParams = ref({})
const orParams = ref({})
const orderParams = ref({})

const doSearch = (tab: any, search: any) => {
    if (currentTab.value === -1) {
        currentTab.value = tab
    }
    searchKey.value = search
    pagingRef.value.reload()
    getArticleList(1, 2)
    getHospitalList(1, 2)
    getNurseList(1, 2)

}
const getHospitalList = (pageNo: any, pageSize: any) => {
    api.hospital.list({
        pageNo: pageNo,
        pageSize: pageSize,
        andParams: JSON.stringify(andParams.value),
        orParams: JSON.stringify({
            'hospital': `%${searchKey.value}%`,
        }),
        orderParams: JSON.stringify(orderParams.value),
    }).then(res => {
        hospitalList.value = res.data.data.records
    })
}
const getNurseList = (pageNo: any, pageSize: any) => {
    api.nurse.formalList({
        pageNo: pageNo,
        pageSize: pageSize,
        andParams: JSON.stringify(andParams.value),
        orParams: JSON.stringify({
            name: `%${searchKey.value}%`,
        }),
        orderParams: JSON.stringify(orderParams.value),
    }).then(res => {
        if (res && res.data.code === 0) {
            nurseList.value = res.data.data.records
        }
    })
}
const getArticleList = (pageNo: any, pageSize: any) => {
    api.article.list({
        pageNo: pageNo,
        pageSize: pageSize,
        andParams: JSON.stringify({
            status: 2
        }),
        orParams: JSON.stringify({
            title: `%${searchKey.value}%`,
            username: `%${searchKey.value}%`
        }),
        orderParams: JSON.stringify({
            createTime: 2
        }),
    }).then(res => {
        if (res && res.data.code === 0) {
            articleList.value = res.data.data.records
        }
    })
}

const queryList = (pageNo: any, pageSize: any) => {
    if (currentTab.value === 0) {
        api.hospital.list({
            pageNo: pageNo,
            pageSize: pageSize,
            andParams: JSON.stringify(andParams.value),
            orParams: JSON.stringify({
                'hospital': `%${searchKey.value}%`,
            }),
            orderParams: JSON.stringify(orderParams.value),
        }).then(res => {
            if (res && res.data.code === 0) {
                let tmp = res.data.data.records
                pagingRef.value.complete(tmp);
            }
        })
    } else if (currentTab.value === 1) {
        api.nurse.formalList({
            pageNo: pageNo,
            pageSize: pageSize,
            andParams: JSON.stringify(andParams.value),
            orParams: JSON.stringify({
                name: `%${searchKey.value}%`,
            }),
            orderParams: JSON.stringify(orderParams.value),
        }).then(res => {
            if (res && res.data.code === 0) {
                let tmp = res.data.data.records
                pagingRef.value.complete(tmp);
            }
        })
    } else if (currentTab.value === 2) {
        api.article.list({
            pageNo: pageNo,
            pageSize: pageSize,
            andParams: JSON.stringify({
                status: 2
            }),
            orParams: JSON.stringify({
                title: `%${searchKey.value}%`,
                username: `%${searchKey.value}%`
            }),
            orderParams: JSON.stringify({
                createTime: 2
            }),
        }).then(res => {
            if (res && res.data.code === 0) {
                let tmp = res.data.data.records
                pagingRef.value.complete(tmp);
            }
        })
    }
}
const toQuery = (item: any) => {
    console.log(item)
    if (currentTab.value === 0) {
        uni.navigateTo({
            url: '/pages/common/introduction/hospital?hospitalInfo=' + encodeURIComponent(JSON.stringify(item))
        })
    } else if (currentTab.value === 1) {
        uni.navigateTo({
            url: '/pages/common/introduction/nurse?nurseInfo=' + encodeURIComponent(JSON.stringify(item))
        })
    } else if (currentTab.value === 2) {
        uni.navigateTo({
            url: '/pages/common/article/article?articleInfo=' + encodeURIComponent(JSON.stringify(item))
        })
    }

}

const tabSelect = (e: any) => {
    currentTab.value = e.currentTarget.dataset.id * 1.0
    console.log(currentTab.value)
    if (currentTab.value === 3) {
        getArticleList(1, 2)
        getHospitalList(1, 2)
        getNurseList(1, 2)
    }
    pagingRef.value.reload()
}
defineExpose({
    doSearch,
})

</script>

<style scoped>

</style>